package com.smartstate.autoMaticControl.websocket;

import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.smartstate.autoMaticControl.config.SpringUtil;
import com.smartstate.autoMaticControl.constant.DeviceTypeEnum;
import com.smartstate.autoMaticControl.entity.ColdSite;
import com.smartstate.autoMaticControl.entity.Dedusting;
import com.smartstate.autoMaticControl.entity.Dehumidifier;
import com.smartstate.autoMaticControl.entity.WebSocketOnMessage;
import com.smartstate.common.web.exception.CommonException;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

public class DateTimeRunnable implements Runnable{

    @Autowired
    private RedisTemplate<String, String> redisTemplate = (RedisTemplate<String, String>) SpringUtil.getBean("redisTemplate");

    private WebSocketOnMessage webSocketOnMessage;

    DateTimeRunnable(){
    }

    public void setWebSocketOnMessage(WebSocketOnMessage webSocketOnMessage){
        this.webSocketOnMessage = webSocketOnMessage;
    }

    private String[] arr={"正常","0","异常"};

    @Override
    public void run() {
        if (StringUtils.isNotEmpty(webSocketOnMessage.getDeviceId()) && webSocketOnMessage.getType().equals(DeviceTypeEnum.DEHUMIDIFIER_TYPE.getValue().toString())) {
            Dehumidifier dehumidifier = new Dehumidifier();
            dehumidifier.setId(UUID.randomUUID().toString());
            dehumidifier.setDeviceId(webSocketOnMessage.getDeviceId());
            dehumidifier.setReturnAirDewPoint(new Random().nextInt(50));
            dehumidifier.setReturnAirTemperature(new Random().nextInt(50));
            dehumidifier.setSupplyAirDewPoint(new Random().nextInt(50));
            dehumidifier.setSupplyAirTemperature(new Random().nextInt(50));
            dehumidifier.setRoomDewPoint(new Random().nextInt(50));
            dehumidifier.setRoomTemperature(new Random().nextInt(50));
            dehumidifier.setType(arr[new Random().nextInt(arr.length)]);
            Map<String, Object> map = BeanUtils.beanToMap(dehumidifier);
            redisTemplate.opsForHash().putAll(dehumidifier.getDeviceId(), map);
        }
        if(StringUtils.isNotEmpty(webSocketOnMessage.getDeviceId()) &&
                webSocketOnMessage.getType().equals(DeviceTypeEnum.DUSTER_TYPE.getValue().toString())){
            Dedusting dedusting = new Dedusting();
            dedusting.setDeviceId(webSocketOnMessage.getDeviceId());
            dedusting.setNeGaTive(new Random().nextInt(50));
            dedusting.setSPraying(new Random().nextInt(50));
            dedusting.setMotorSpeed(new Random().nextInt(50));
            dedusting.setType(arr[new Random().nextInt(arr.length)]);
            Map<String, Object> map = BeanUtils.beanToMap(dedusting);
            try {
                redisTemplate.opsForHash().putAll(dedusting.getDeviceId(),map);
            }catch (Exception e){
                throw new CommonException("除尘数据添加失败");
            }
        }
        if(StringUtils.isNotEmpty(webSocketOnMessage.getDeviceId()) &&
                webSocketOnMessage.getType().equals(DeviceTypeEnum.COLD_SITE_TYPE.getValue().toString())){
            ColdSite coldSite = new ColdSite();
            coldSite.setOwTemp(RandomStringUtils.randomAscii(4));
            coldSite.setPumpStatus(RandomStringUtils.randomAscii(4));
            coldSite.setValveOpening(RandomStringUtils.randomAscii(4));
            coldSite.setOwPressure(RandomStringUtils.randomAscii(4));
            coldSite.setAscentFlow(RandomStringUtils.randomAscii(4));
            coldSite.setPumpLoad(RandomStringUtils.randomAscii(4));
            coldSite.setPumpFrequency(RandomStringUtils.randomAscii(4));
            coldSite.setWaterTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setInletTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setCoalPressureLN(RandomStringUtils.randomAscii(4));
            coldSite.setCoalTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setOilPressureYS(RandomStringUtils.randomAscii(4));
            coldSite.setOilTempYS(RandomStringUtils.randomAscii(4));
            coldSite.setGvOpeningYS(RandomStringUtils.randomAscii(4));
            coldSite.setElectricity(RandomStringUtils.randomAscii(4));
            coldSite.setEleFeedback(RandomStringUtils.randomAscii(4));
            coldSite.setRunTime(RandomStringUtils.randomAscii(4));
            coldSite.setWaterTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setInletTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoalPressureZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoalTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoilTemp(RandomStringUtils.randomAscii(4));
            coldSite.setBearingTemp(RandomStringUtils.randomAscii(4));
            coldSite.setGasTemp(RandomStringUtils.randomAscii(4));
            coldSite.setRwTemp(RandomStringUtils.randomAscii(4));
            coldSite.setRwPressure(RandomStringUtils.randomAscii(4));
            coldSite.setJellyStatus(RandomStringUtils.randomAscii(4));
            coldSite.setChillingStatus(RandomStringUtils.randomAscii(4));
            coldSite.setChillingLoad(RandomStringUtils.randomAscii(4));
            coldSite.setChillingFrequency(RandomStringUtils.randomAscii(4));
            coldSite.setDosingStatus(RandomStringUtils.randomAscii(4));
            coldSite.setMoisturizingStatus(RandomStringUtils.randomAscii(4));
            coldSite.setType(arr[new Random().nextInt(arr.length)]);
            Map<String, Object> map = com.baomidou.mybatisplus.core.toolkit.BeanUtils.beanToMap(coldSite);
            redisTemplate.opsForHash().putAll(webSocketOnMessage.getDeviceId(),map);
        }
    }
}
